package net.arvin.service;

import net.arvin.dao.ResponseHandler;
import net.arvin.utils.HttpRequestClient;
import net.arvin.view.LoadingView;

import org.apache.http.Header;

import android.content.Context;
import android.widget.Toast;

import com.loopj.android.http.TextHttpResponseHandler;

public class CardService {
	public static void tryBindCard(final String cardJson,
			final ResponseHandler handler, final Context context) {
		LoadingView.show(context, "绑定中...");
		HttpRequestClient.startPostBindCardRequest(cardJson,
				new TextHttpResponseHandler() {
					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseBody, Throwable error) {
						LoadingView.dismiss();
						if (responseBody == null) {
							Toast.makeText(context, "连接超时=_=!",
									Toast.LENGTH_SHORT).show();
						}
					}

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							String responseBody) {
						LoadingView.dismiss();
						if (responseBody == null) {
							Toast.makeText(context, "服务器出错啦=_=!",
									Toast.LENGTH_SHORT).show();
						} else {
							if (responseBody.equals("")) {
								handler.onRequestFailed(responseBody);
							} else {
								handler.onRequsetSucceeded(responseBody);
							}
						}
					}
				}, context);
	}

	public static void tryGetAllCards(final String phonenumber,
			final ResponseHandler handler, final Context context) {
		HttpRequestClient.startPostGetAllCardsRequest(phonenumber,
				new TextHttpResponseHandler() {
					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseBody, Throwable error) {
						if (responseBody == null) {
							Toast.makeText(context, "未获取到数据=_=!",
									Toast.LENGTH_SHORT).show();
						}
					}

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							String responseBody) {
						if (responseBody == null) {
							Toast.makeText(context, "服务器出错啦=_=!",
									Toast.LENGTH_SHORT).show();
						} else {
							if (responseBody.equals("")) {
								handler.onRequestFailed(responseBody);
							} else {
								handler.onRequsetSucceeded(responseBody);
							}
						}
					}
				}, context);
	}

	public static void tryUnbindCard(final String cardnumber,
			final ResponseHandler handler, final Context context) {
		LoadingView.show(context, "解除绑定中...");
		HttpRequestClient.startPostUnbindCardRequest(cardnumber,
				new TextHttpResponseHandler() {
					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseBody, Throwable error) {
						LoadingView.dismiss();
						if (responseBody == null) {
							Toast.makeText(context, "连接超时=_=!",
									Toast.LENGTH_SHORT).show();
						}
					}

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							String responseBody) {
						LoadingView.dismiss();
						if (responseBody == null) {
							Toast.makeText(context, "服务器出错啦=_=!",
									Toast.LENGTH_SHORT).show();
						} else {
							if (responseBody.equals("")) {
								handler.onRequestFailed(responseBody);
							} else {
								handler.onRequsetSucceeded(responseBody);
							}
						}
					}
				}, context);
	}

}
